package logic

import (
	"context"
	"time"

	"mall/service/user/model"
	"mall/service/user/rpc/internal/svc"
	"mall/service/user/rpc/types/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserAccountLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateUserAccountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserAccountLogic {
	return &UpdateUserAccountLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateUserAccountLogic) UpdateUserAccount(in *user.UpdateUserAccountReq) (*user.UpdateUserAccountResp, error) {
	if in.Level == 0 {
		return &user.UpdateUserAccountResp{}, nil
	}

	filterMap := make(map[string]map[string][]interface{})
	filterMap[model.UserAccountColumns.UserID] = map[string][]interface{}{
		"equalTo": []interface{}{in.UserId},
	}
	cols := model.M{
		model.UserAccountColumns.UpdatedAt: time.Now(),
	}
	if in.Level > 0 {
		cols[model.UserAccountColumns.Level] = uint8(in.Level)
	}
	_, err := l.svcCtx.CurrentUser.UpdateAccountBatch(l.ctx, nil, cols, filterMap)
	if err != nil {
		return nil, err
	}

	return &user.UpdateUserAccountResp{}, nil
}
